﻿using System;
using Microsoft.Xna.Framework;

namespace Serious
{
    public class CylinderFollower : Camera3D
    {
        public Cylinder target;
        public Vector2 vertical_angle;
        public Single dist;
        public Single z_offset;

        public override Vector3 to
        {
            get
            {
                Vector3 target = this.target.position;
                target.Z += z_offset;
                return target;
            }
        }
        public override Vector3 from
        {
            get
            {
                Vector3 target = this.to;
                Vector2 angdir = -Vector2Ext.FromDirectionDeg(this.target.angle);
                Vector3 pos = target + dist * new Vector3(vertical_angle.X * angdir.X, vertical_angle.X * angdir.Y, vertical_angle.Y);
                return pos;
            }
        }
        public override Vector3 up
        {
            get
            {
                Vector2 dd = Vector2Ext.FromDirectionDeg(this.target.angle);
                Vector2 ddz = vertical_angle;
                ddz.Y *= -1.0f;
                Vector3 forward = new Vector3(dd.X * ddz.X, dd.Y * ddz.X, ddz.Y);
                Vector3 side = new Vector3(dd.Rotated(Vector2.UnitY), 0.0f);
                Vector3 up = Vector3.Cross(forward, side);
                //up = Vector3.UnitZ;
                return up;
            }
        }

        public override Matrix ViewMatrix
        {
            get
            {
                return Matrix.CreateLookAt(this.from, this.to, this.up);
            }
        }

        public CylinderFollower (Cylinder target, Vector2 vertical_angle, Single dist, Single z_offset)
        {
            this.target = target;
            this.vertical_angle = vertical_angle;
            this.dist = dist;
            this.z_offset = z_offset;
        }
    }
}
